<?php


class Sherpa_Model_Products extends WSB_Model_Collection
{

	/**
	 * @var Sherpa_Model_Product
	 */
	protected $_modelClass = 'Sherpa_Model_Product';

	/**
	 * @var array Hashtabelle mit key=ProductId und value=Index
	 */
	private $_idHashTable = array();

	/**
	 * @return void
	 */
	public function init()
	{
		foreach($this->_data AS $key=>$product) {
			$id = $product['id'];
			$this->_idHashTable[$id] = $key;
		}
	}

	/**
	 * @return array
	 */
	public function getIds()
	{
		return array_keys($this->_idHashTable);
	}

	/**
	 * @param string $currencyCode
	 */
	public function loadParentProductPrices($currencyCode)
	{
		// Preise holen und mit Produkten zusammenf�hren
		require_once(WSB_MODULES_PATH .'sherpa/models/PriceGateway.php');
		$gateway = new Sherpa_Model_PriceGateway();
		$cache = $this->getCacheInstance($gateway);
		// Das Cache-Objekt ruft intern die Methode Sherpa_Model_PriceGateway::fetchPrices auf
		$prices = $cache->fetchPrices($this->getIds(), $currencyCode);
		$this->_mergePrices($prices);
	}

	/**
	 * @param integer $languageId
	 */
	public function loadParentProductImages($languageId)
	{
		// Bilder holen und mit Produkten zusammenf�hren
		require_once(WSB_MODULES_PATH .'sherpa/models/ImageGateway.php');
		$gateway = new Sherpa_Model_ImageGateway();
		$cache = $this->getCacheInstance($gateway);
		// Das Cache-Objekt ruft intern die Methode Sherpa_Model_ImageGateway::fetchParentProductMainImages auf
		$images = $cache->fetchParentProductMainImages($this->getIds(), $languageId);
		$this->_mergeImages($images);
	}

	/**
	 * @return void
	 */
	public function loadParentProductColorApplications()
	{
		// ColorApplications holen und mit Produkten zusammenf�hren
		require_once(WSB_MODULES_PATH .'sherpa/models/ColorApplicationGateway.php');
		$gateway = new Sherpa_Model_ColorApplicationGateway();
		$cache = $this->getCacheInstance($gateway);
		// Das Cache-Objekt ruft intern die Methode Sherpa_Model_ColorApplicationGateway::fetchParentProductColorApplications auf
		$colorApplications = $cache->fetchParentProductColorApplications($this->getIds());
		$this->_mergeColorApplications($colorApplications);
	}

	/**
	 * @param Sherpa_Model_BaseGateway $cachedEntity
	 * @return Zend_Cache_Frontend_Class
	 */
	protected function getCacheInstance(Sherpa_Model_BaseGateway $cachedEntity)
	{
		$frontendOptions = array(
		   'cached_entity' => $cachedEntity
		);
		$backendOptions = array('cache_dir' => WSB_CACHE_DIR . 'sherpa');
		$cache = Zend_Cache::factory('Class', 'File', $frontendOptions, $backendOptions);
		return $cache;
	}

	/**
	 * @param Sherpa_Model_Prices $prices
	 */
	private function _mergePrices(Sherpa_Model_Prices $prices)
	{
		foreach($prices AS $price) {
			$productId = $price->productId;
			$key = $this->_getKeyForProductId($productId);
			if(!is_null($key)) {
				// price, oldPrice, currencyCode
				$this->_data[$key]['price'] = $price->price;
				$this->_data[$key]['oldPrice'] = $price->oldPrice;
				$this->_data[$key]['currencyCode'] = $price->currencyCode;
			}
		}
	}

	/**
	 * @param array $images
	 */
	private function _mergeImages(array $images)
	{
		foreach($images AS $image) {
			$parentProductId = $image['parentProductId'];
			$key = $this->_getKeyForProductId($parentProductId);
			if(!is_null($key)) {
				// imageFile
				$this->_data[$key]['imageFile'] = $image['fileName'];
				$this->_data[$key]['imageId'] = $image['id'];
			}
		}
	}

	/**
	 * @param Sherpa_Model_ColorApplications $colorApplications
	 */
	private function _mergeColorApplications(array $colorApplications)
	{
		foreach($colorApplications AS $colorApplication) {
			$parentProductId = $colorApplication['parentProductId'];
			$key = $this->_getKeyForProductId($parentProductId);
			if(!is_null($key)) {
				if(!array_key_exists('colorApplications', $this->_data[$key])) {
					$this->_data[$key]['colorApplications'] = array();
				}
				$this->_data[$key]['colorApplications'][] = $colorApplication;
			}
		}
	}

	/**
	 * @param integer $productId
	 * @return integer
	 */
	private function _getKeyForProductId($productId)
	{
		return isset($this->_idHashTable[$productId]) ? $this->_idHashTable[$productId] : null;
	}

}
